#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#define REP(i,n) for(int i=0;i<int(n);++i)

using namespace std;

typedef long long lld;

class Widgets
{
	public:
	static const int VBOX = 1,HBOX = 2, OTHER = 3;
	string name;
	lld width,high;
	int border,spacing;
	int type;
	bool wUpdated,hUpdated;
	vector<Widgets*> component;
	Widgets(string s,int t,int w = 0,int h = 0)
	{
		name = s;
		type = t;
		width = w, high = h;
		border = spacing = 0;
		component.clear();
		wUpdated = hUpdated = false;
	}
	lld getWidth()
	{
		if(type==OTHER||wUpdated) return width;
		if(component.empty()) return 0;
		else if(type==HBOX)
		{
			lld t = 0;
			REP(i,component.size())
			{
				if(i) t+=spacing;
				t+= component[i]->getWidth();
			}
			wUpdated = true;
			return width=t+2*border;
		}
		else
		{
			lld t = -1;
			REP(i,component.size())
			{
				t = max(t,component[i]->getWidth());
			}
			wUpdated = true;
			return width=t+2*border;
		}
	}
	lld getHigh()
	{
		if(type==OTHER||hUpdated) return high;
		if(component.empty()) return 0;
		else if(type==VBOX)
		{
			lld t = 0;
			REP(i,component.size())
			{
				if(i) t+=spacing;
				t+= component[i]->getHigh();
			}
			hUpdated = true;
			return high=t+2*border;
		}
		else
		{
			lld t = -1;
			REP(i,component.size())
			{
				t = max(t,component[i]->getHigh());
			}
			hUpdated = true;
			return high=t+2*border;
		}
	}
};

vector<Widgets*> widgets;

Widgets* getWidgets(string name)
{
	REP(i,widgets.size()) if(widgets[i]->name==name)
		return widgets[i];
	printf("error!! find name: %s \n",name.c_str());
}

bool cmp(Widgets *a,Widgets *b)
{
	return a->name < b->name;
}
void go(string s)
{
	char name[20]={0};
	if(s.find(".")==string::npos)
	{
		if(s=="Widget")
		{
			int width,high;
			scanf("%*[^a-z]%[a-z](%d,%d)",name,&width,&high);
			widgets.push_back(new Widgets(name,Widgets::OTHER,width,high));
		}
		else
		{
			cin>>name;
			if(s == "HBox") widgets.push_back(new Widgets(name,Widgets::HBOX));
			else widgets.push_back(new Widgets(name,Widgets::VBOX));
		}
	}
	else
	{
		char opr[20],name2[20];
		sscanf(s.c_str(),"%[a-z].%[^(](%[^)])",name,opr,name2);
		//printf("name = %s opr = %s name2 = %s\n",name,opr,name2);
		s = opr;
		if(s=="pack")
		{
			Widgets *father,*child;
			father = getWidgets(name);
			child = getWidgets(name2);
			father->component.push_back(child);
		}
		else
		{
			Widgets *ins = getWidgets(name);
			int t;
			sscanf(name2,"%d",&t);
			if(s=="set_border") ins->border = t;
			else ins->spacing = t;
		}
	}
}
int main()
{
	int n;
	string ins;
	cin>>n;
	while(n--)
	{
		cin>>ins;
		go(ins);
	}
	sort(widgets.begin(),widgets.end(),cmp);
	REP(i,widgets.size()) printf("%s %I64d %I64d\n",
								widgets[i]->name.c_str(),
								widgets[i]->getWidth(),
								widgets[i]->getHigh());
	return 0;
}
